package com.example.interactservice.controller;

import com.example.commom.enums.RespDescEnum;
import com.example.commom.enums.StatusCodeEnum;
import com.example.commom.utils.JwtUtils;
import com.example.feign.result.AjaxResult;
import com.example.interactservice.service.FollowService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 软工2104-李家强-2100770248
 * @since 2023-09-27
 */
@RestController
@RequestMapping("/follow")
public class FollowController {

    @Autowired
    private FollowService followService;

    /**
     *  关注 / 取关操作
     */
    @PostMapping("/{id}/{isFollow}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要关注的用户id", dataType = "long",
                    paramType = "path", required = true),
            @ApiImplicitParam(name = "isFollow", value = "true: 关注, false: 取关", dataType = "boolean",
                    paramType = "path", required = true)
    })
    @ApiOperation(value = "关注 / 取关操作", notes = "参数是要关注的用户id和一个布尔值")
    public AjaxResult follow(@PathVariable(value = "id") Long followUserId,
                                    @PathVariable(value = "isFollow") boolean isFollow,
                                    @RequestHeader(value = "token") String token) {

        // 获取登录 id
        String userId = JwtUtils.getUserIdByToken(token);
        if (userId == null) {
            return AjaxResult.fail(StatusCodeEnum.Redirect.getCode(), StatusCodeEnum.Redirect.getMsg());
        }

        return followService.follow(followUserId, isFollow, userId);
    }

    /**
     *  查看是否关注了指定用户
     */
    @GetMapping("/isFollow/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要关注的用户id", dataType = "long",
                    paramType = "path", required = true),
    })
    @ApiOperation(value = "查看是否关注了指定用户", notes = "目标用户id")
    public AjaxResult isFollow(@PathVariable("id") String followUserId,
                               @RequestHeader("token") String token) {

        // 1. 拿到登录用户 id
        String userId = JwtUtils.getUserIdByToken(token);
        if (userId == null) {
            return AjaxResult.fail(StatusCodeEnum.Redirect.getCode(), StatusCodeEnum.Redirect.getMsg());
        }

        return followService.isFollow(followUserId, userId);
    }

}
